package utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

import javax.servlet.ServletContext;

import com.sun.net.httpserver.HttpContext;

public class DBConnector {
	
	private Connection mConnection;
	// TODO: use this context, this is set when web application is initiated
	private static ServletContext mContext;
	
	public DBConnector ()  throws ClassNotFoundException, SQLException {
		String DBServer_user = mContext.getInitParameter("DBUser");
		String DBServer_password = mContext.getInitParameter("DBPass");
		String DBServer_url = mContext.getInitParameter("DBUrl");
		String DBServer_driver = mContext.getInitParameter("DBDriver");
				
		Class.forName(DBServer_driver); 
		 
        //Create the connection using the static getConnection method 
		mConnection = DriverManager.getConnection (DBServer_url, DBServer_user, DBServer_password); 
	}
	
	public Connection getConnection() {
		return mConnection;
	}
	
	public static void setContext(ServletContext context) {
		mContext = context;
	}
	
	public void close() throws SQLException {
		//mConnection.commit();
		mConnection.close();
		mConnection = null;		
	}
}
